Imports System
Imports System.Data
Imports System.Data.SqlClient
Imports System.Runtime.InteropServices
Imports System.Configuration
Imports System.Text
Public Class FeedBackAdmin
    Public DBConnectstring As String
    Public RecipientID As Integer
    Public DescriptionID As Integer
    Public NextDisplayOrder As Integer
    Public DescriptionText As String
    Public CID As String
    Public sEmailaddress As String
    Public sRealName As String
    'Private Cmd As SqlCommand
    Private Rs As DataSet
    'Private Conn As SqlConnection
    Public ErrMsg As String


    Public Function DeleteFeedbkConts(ByVal sPageid As String) As String

        Dim sSQL As String = ""
        Dim sErr As String = ""
        'Conn = New SqlConnection
        'Conn.Open(DBConnectstring)
        If sPageid = "fbadminemail" Then
            sSQL = "delete from FEEDBACK_RECIPIENTS where RecipientID =" & RecipientID & ""
            CallExecuteSQL(DBConnectstring, sSQL, sErr)
        End If
        If sPageid = "fbadmindesc" Then
            Rs = New DataSet
            sSQL = "SELECT DescriptionID from FEEDBACK where DescriptionID = " & DescriptionID

            GetDataset(DBConnectstring, sSQL, "feedbck", sErr, Rs)

            If Rs.Tables(0).Rows.Count > 0 And Len(sErr) = 0 Then
                sSQL = ""
                sSQL = "delete from FEEDBACK_DESCRIPTIONS where DescriptionID = " & DescriptionID & ""
                CallExecuteSQL(DBConnectstring, sSQL, sErr)
            End If
        End If
        If Len(sErr) > 0 Then
            Return sErr
        Else
            Return "OK"
        End If


    End Function

    Public Function AddConts(ByVal sPageid As String) As Boolean

        Dim bAddok As Boolean
        Dim conn As SqlConnection = New SqlConnection(DBConnectstring)
        conn.Open()
        'Dim Cmd As SqlCommand = New SqlCommand("InsFeedBackRecipients", conn)
        conn.Open()
        ''Conn.Open(DBConnectstring)
        If sPageid = "fbadminemail" Then
            Try
                Dim Cmd As SqlCommand = New SqlCommand("InsFeedBackRecipients", conn)
                With Cmd

                    .CommandType = CommandType.StoredProcedure
                    '.Parameters.Add("@i_cid", CInt(CID))
                    '.Parameters.Add("@i_emailaddress", Trim(sEmailaddress))
                    '.Parameters.Add("@i_realname", Trim(sRealName))
                    '.Parameters.Add("@o_errorMsg", Nothing)
                    .Parameters(3).SqlDbType = SqlDbType.VarChar
                    .Parameters(3).Direction = ParameterDirection.Output

                End With

                Cmd.ExecuteNonQuery()

                '------------------------------------------------------------------------
                'Returns a message to the calling ASP page.
                '------------------------------------------------------------------------
                If Not IsNothing(Cmd.Parameters("o_ERRORMSG").Value) Then
                    ''Debug.Print("Error Msg = " & Cmd.Parameters("o_ERRORMSG"))
                    ErrMsg = Cmd.Parameters("o_ERRORMSG").Value
                End If
                conn.Close()

            Catch ex As SqlException
                
                Dim i As Integer = 0

                For i = 0 To ex.Errors.Count - 1
                    ErrMsg = ex.Errors(i).Message.ToString()
                Next i
                ''/**use this code to debug**//

                LogErrorEvent(ErrMsg)

            Finally
                If Not (conn Is Nothing) Then
                    conn.Close()
                End If
            End Try

        End If
        If sPageid = "fbadmindesc" Then
            Try
                Dim Cmd As SqlCommand = New SqlCommand("InsFeedbackDescriptions", conn)
                With Cmd
                    .CommandType = CommandType.StoredProcedure
                    '.Parameters.Add("@i_descriptionText", DescriptionText)
                    '.Parameters.Add("@i_displayorder", Trim(NextDisplayOrder))
                    .Parameters.Add("@o_errorMsg", Nothing)
                    .Parameters(3).SqlDbType = SqlDbType.VarChar
                    .Parameters(3).Direction = ParameterDirection.Output
                End With


                Cmd.ExecuteNonQuery()
                '------------------------------------------------------------------------
                'Returns a message to the calling ASP page.
                '------------------------------------------------------------------------
                If Not IsNothing(Cmd.Parameters("o_errorMsg").Value) Then
                    ErrMsg = Cmd.Parameters("o_errorMsg").Value

                End If
                '------------------------------------------------------------------------
                conn.Close()
            Catch ex As SqlException
                Dim i As Integer = 0

                For i = 0 To ex.Errors.Count - 1
                    ErrMsg = ex.Errors(i).Message.ToString()
                Next i
                LogErrorEvent(ErrMsg)

            Finally
                If Not (conn Is Nothing) Then
                    conn.Close()
                End If
            End Try
        End If
        If Len(ErrMsg) = 0 Or IsNothing(ErrMsg) Then
            bAddok = True
        Else
            bAddok = False
        End If
        Return bAddok

    End Function

    Public Function GetCurrentConts(ByVal sPageid As String) As DataSet
        Dim sErr As String = ""
        Dim sSQL As String = ""
       
        Rs = New DataSet
       
        If sPageid = "fbadminemail" Then
            sSQL = "SELECT * FROM FEEDBACK_RECIPIENTS"
            CallExecuteSQL(DBConnectstring, sSQL, sErr)
        End If

        If sPageid = "fbadmindesc" Then
            sSQL = "SELECT * from FEEDBACK_DESCRIPTIONS order by DisplayOrder"
            GetDataset(DBConnectstring, sSQL, "feedbck", sErr, Rs)
        End If
        'Rs.Open(sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText)
        If IsNothing(sErr) Then
            If Rs.Tables(0).Rows.Count > 0 Then
                GetCurrentConts = Rs

            Else
                LogErrorEvent(sErr)
                GetCurrentConts = Nothing
            End If

        Else
            LogErrorEvent(sErr)
            GetCurrentConts = Nothing
        End If

    End Function
    Public Function GetDetailConts() As DataSet

        Dim sSQL As String = ""
        Dim sErr As String = ""
        
        Rs = New DataSet
        
        sSQL = sSQL & "select "
        sSQL = sSQL & "     FEEDBACK.GAPORTAL_ID, "
        sSQL = sSQL & "     FEEDBACK.Company_ID, "
        sSQL = sSQL & "     FEEDBACK.EntryDate, "
        sSQL = sSQL & "     FEEDBACK.URL, "
        sSQL = sSQL & "     FEEDBACK.EmailAddress, "
        sSQL = sSQL & "     FEEDBACK_DESCRIPTIONS.DescriptionText, "
        sSQL = sSQL & "     FEEDBACK.MessageText, "
        sSQL = sSQL & "     FEEDBACK.BrowserOS, "
        sSQL = sSQL & "     FEEDBACK.ReferringURL, "
        sSQL = sSQL & "     FEEDBACK.Company "
        sSQL = sSQL & "from "
        sSQL = sSQL & "     FEEDBACK, FEEDBACK_DESCRIPTIONS "
        sSQL = sSQL & "where "
        sSQL = sSQL & "     FEEDBACK.DescriptionID = FEEDBACK_DESCRIPTIONS.DescriptionID "
        sSQL = sSQL & "order by "
        sSQL = sSQL & "     FEEDBACK.EntryDate desc"
        Try
            GetDataset(DBConnectstring, sSQL, "feedbck", sErr, Rs)
            If Rs.Tables(0).Rows.Count > 0 Then
                GetDetailConts = Rs

            Else
                LogErrorEvent(sErr)
                Return Nothing
            End If
        Catch ex As SqlException
            Dim i As Integer = 0

            For i = 0 To ex.Errors.Count - 1
                ErrMsg = ex.Errors(i).Message.ToString()
            Next i
            LogErrorEvent(ErrMsg)

        
        End Try
        Return Rs
    End Function
    Public Sub MoveUpDown(ByVal DescriptionID As Integer, ByVal Derection As String)
        'On Error GoTo ErrorHandler
        Dim rst As DataSet
        Dim Conn As SqlConnection
        Dim i As Integer
        Dim nSelInd As Integer
        Dim nTarInd As Integer
        Dim nTempVar As Integer
        Dim sSQL As String = ""
        Dim sErr As String = ""
        Dim aDescriptions() As Integer = Nothing
        Conn = New SqlConnection

        rst = New DataSet
        sSQL = "select DescriptionID from FEEDBACK_DESCRIPTIONS order by DisplayOrder"
        'rst.Open(sSQL, Conn, adOpenForwardOnly, adLockReadOnly, adCmdText)
        GetDataset(DBConnectstring, sSQL, "feedbck", sErr, rst)
        If rst.Tables(0).Rows.Count > 0 Then
            i = 0
            i = rst.Tables(0).Rows.Count
            'Do While Not rst.EOF
            '    i = i + 1
            '    rst.MoveNext()
            'Loop
            ReDim aDescriptions(i)

            'rst.Requery()
            i = 0
            For i = 0 To rst.Tables(0).Rows.Count - 1
                ' Do While Not rst.EOF
                aDescriptions(i) = CInt(rst.Tables(0).Rows(i).Item("DescriptionID"))
            Next
            '    i = i + 1
            '    rst.MoveNext()
            'Loop
        End If
        'rst.ActiveConnection = Nothing

        If Not IsNothing(aDescriptions) Then
            '-----  initializing vars
            nSelInd = 0    '-----  array index of selected Description ID
            nTarInd = 0    '-----  array index of Description ID that will swap places with selected.
            '-----  populating nSelInd
            For i = 0 To UBound(aDescriptions) - 1 Step 1
                If aDescriptions(i) = DescriptionID Then
                    nSelInd = i
                    Exit For
                End If
            Next
            '-----  populating nTarInd
            If Derection = "up" Then
                If nSelInd = 0 Then
                    nTarInd = UBound(aDescriptions) - 1
                Else
                    nTarInd = nSelInd - 1
                End If
            ElseIf Derection = "down" Then
                If nSelInd = UBound(aDescriptions) - 1 Then
                    nTarInd = 0
                Else
                    nTarInd = nSelInd + 1
                End If
            End If

            If nSelInd <> nTarInd Then
                nTempVar = aDescriptions(nTarInd)   '-----  saving target index Description ID
                aDescriptions(nTarInd) = aDescriptions(nSelInd) '-----  moving selected Description to new display order
                aDescriptions(nSelInd) = nTempVar   '-----  moving swapped Description to selected Description's original location.
                '-----  re-generating display order values
                For i = 0 To UBound(aDescriptions) - 1 Step 1
                    sSQL = ""
                    sSQL = sSQL & "UPDATE FEEDBACK_DESCRIPTIONS "
                    sSQL = sSQL & "SET DisplayOrder = " & i + 1 & " "
                    sSQL = sSQL & "WHERE DescriptionID = " & aDescriptions(i) & " "
                    CallExecuteSQL(DBConnectstring, sSQL, sErr)

                Next

            End If
        End If
        Exit Sub


    End Sub


End Class
